package com.wanggsx.pluginlib;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewGroup;

/** 代理activity */
public class ProxyActivity extends Activity {
    //要加载的activit的类名
    private String mClassName;
    //
    private PluginApk mPluginApk;
    private IPlugin mPluginActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mClassName = getIntent().getStringExtra("CLASSNAME");
        mPluginApk = PluginManager.getInstance().getmPluginApk();
        try {
            launchActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void launchActivity() throws Exception {
        if (mPluginApk == null){
            throw new RuntimeException("未加载插件apk");
        }
        Class<?> clazz = mPluginApk.mClassLoader.loadClass(mClassName);
        Object obj = clazz.newInstance();
        //只有插件apk才被调用
        if (obj instanceof IPlugin){
            mPluginActivity = (IPlugin) obj;
            //将代理activity对象传给被代理activity
            mPluginActivity.attach(this);
            Bundle bundle = new Bundle();
            bundle.putInt("FROM",IPlugin.FROM_EXTERNAL);
            mPluginActivity.onCreate(bundle);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    protected void onStart() {
        mPluginActivity.onStart();
        super.onStart();
    }

    @Override
    protected void onRestart() {
        mPluginActivity.onRestart();
        super.onRestart();
    }

    @Override
    protected void onResume() {
        mPluginActivity.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mPluginActivity.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        mPluginActivity.onStop();
        super.onStop();
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data){
        mPluginActivity.onActivityResult(requestCode,resultCode,data);
    }
    public void onDestroy(){
        mPluginActivity.onDestroy();
        super.onDestroy();
    }

    public void onSaveInstanceState(Bundle outState){
        mPluginActivity.onSaveInstanceState(outState);
    }
    public void onNewIntent(Intent intent){
        mPluginActivity.onNewIntent(intent);
    }
    public void onRestoreInstanceState(Bundle savedInstanceState){
        mPluginActivity.onRestoreInstanceState(savedInstanceState);
    }
    public boolean onTouchEvent(MotionEvent event){
        return mPluginActivity.onTouchEvent(event);
    }
    public boolean onKeyUp(int keyCode, KeyEvent event){
        return mPluginActivity.onKeyUp(keyCode,event);
    }
    public void onWindowAttributesChanged(ViewGroup.LayoutParams params){
        mPluginActivity.onWindowAttributesChanged(params);
    }
    public void onWindowFocusChanged(boolean hasFocus){
        mPluginActivity.onWindowFocusChanged(hasFocus);
    }
    public void onBackPressed(){
        mPluginActivity.onBackPressed();
    }

    @Override
    public Resources getResources() {
        return mPluginApk!=null?mPluginApk.mResouces:super.getResources();
    }

    @Override
    public AssetManager getAssets() {
        return mPluginApk!=null?mPluginApk.mAssertMan:super.getAssets();
    }

    @Override
    public ClassLoader getClassLoader() {
        return mPluginApk!=null?mPluginApk.mClassLoader:super.getClassLoader();
    }
}
